package jk

import (
	"fmt"
	"net/http"
)

type Error interface {
	// GetStatusCode 获取http状态码
	GetStatusCode() int
	// GetCode 获取错误码
	GetCode() int
	// GetMsg 获取错误消息内容
	GetMsg() string
	// String 错误信息
	String() string
	// IsEQ 判断错误信息是否相等
	IsEQ(Error) bool
}

var _ Error = new(httpError)

type httpError struct {
	statusCode int
	code       int
	msg        string
}

func (h *httpError) GetStatusCode() int {
	return h.statusCode
}

func (h *httpError) GetCode() int {
	return h.code
}

func (h *httpError) GetMsg() string {
	return h.msg
}

func (h *httpError) String() string {
	return fmt.Sprintf("{statusCode: %d, code: %d, msg: %s}", h.statusCode, h.code, h.msg)
}

func (h *httpError) IsEQ(e Error) bool {
	return h.GetStatusCode() == e.GetStatusCode() && h.GetCode() == e.GetCode() && h.GetMsg() == e.GetMsg()
}

func NewHttpError(statusCode int, code int, msg string) Error {
	return &httpError{
		statusCode: statusCode,
		code:       code,
		msg:        msg,
	}
}

var Err404 = NewHttpError(http.StatusNotFound, http.StatusNotFound, "请求的资源不存在！")
var Err401 = NewHttpError(http.StatusUnauthorized, http.StatusUnauthorized, "身份验证失败！")
var Err403 = NewHttpError(http.StatusForbidden, http.StatusForbidden, "无权限执行该请求！")

var ErrParams = NewHttpError(http.StatusOK, http.StatusBadRequest, "缺少参数或参数错误！")
var ErrRecordNotFound = NewHttpError(http.StatusOK, http.StatusNotFound, "相关记录不存在，或请稍后再试！")
var ErrUnauthorized = NewHttpError(http.StatusOK, http.StatusUnauthorized, "身份验证失败，请重新登录！")
var ErrServerError = NewHttpError(http.StatusOK, http.StatusInternalServerError, "系统忙，请稍后再试！")
var ErrForbidden = NewHttpError(http.StatusOK, http.StatusForbidden, "无权限执行该请求！")
var ErrBusy = NewHttpError(http.StatusOK, http.StatusServiceUnavailable, "系统忙，请稍后再试！")
var ErrTimeout = NewHttpError(http.StatusOK, http.StatusRequestTimeout, "请求超时，请稍后再试！")
var ErrGoneDelete = NewHttpError(http.StatusOK, http.StatusGone, "相关记录已不存在！")
var ErrBodyTooLarge = NewHttpError(http.StatusOK, http.StatusRequestEntityTooLarge, "请求体过大！")
var ErrTooManyRequests = NewHttpError(http.StatusOK, http.StatusTooManyRequests, "请求次数过多！")
var ErrUpgradeRequired = NewHttpError(http.StatusOK, http.StatusUpgradeRequired, "请升级客户端！")
var ErrUnavailableForLegalReasons = NewHttpError(http.StatusOK, http.StatusUnavailableForLegalReasons, "禁止访问！")
